﻿using FileZillaConfig.Helpers;
using System.Collections.Generic;
using System.Xml.Linq;

namespace FileZillaConfig.ConfigClasses
{
    public class Folder
    {
        private string _path;
        private bool _isHome;
        private Permission _permission;
        private List<string> _aliases;
        private bool _autocreate;

        #region PROPERTIES
        /// <summary>
        /// Only avaiable in group folders
        /// </summary>
        public bool Autocreate
        {
            get { return _autocreate; }
            set { _autocreate = value; }
        }


        /// <summary>
        /// Folder aliases
        /// </summary>
        public List<string> Aliases
        {
            get { return _aliases; }
            set { _aliases = value; }
        }


        /// <summary>
        /// Folder access permissions
        /// </summary>
        public Permission Permissions
        {
            get { return _permission; }
            set { _permission = value; }
        }


        /// <summary>
        /// Selects this folder as home folder
        /// </summary>
        public bool IsHome
        {
            get { return _isHome; }
            set { _isHome = value; }
        }


        /// <summary>
        /// Folder path 
        /// </summary>
        public string Path
        {
            get { return _path; }
            set { _path = value; }
        } 
        #endregion

        public Folder(string path)
        {
            this._path = path;
            this._aliases = new List<string>();
        }

        public XElement getXML()
        {
            XElement Perm = new XElement("Permission", new XAttribute("Dir", this._path));

            if (this._aliases.Count > 0)
            {
                XElement aliases = new XElement("Aliases");
                foreach (string alias in this._aliases)
                {
                    aliases.Add(new XElement("Alias", alias));
                }
                Perm.Add(aliases);
            }

            Perm.Add(XMLItem.Option("FileRead", this._permission.HasFlag(Permission.FileRead)?true:false));
            Perm.Add(XMLItem.Option("FileWrite", this._permission.HasFlag(Permission.FileWrite) ? true : false));
            Perm.Add(XMLItem.Option("FileDelete", this._permission.HasFlag(Permission.FileDelete) ? true : false));
            Perm.Add(XMLItem.Option("FileAppend", this._permission.HasFlag(Permission.FileAppend) ? true : false));
            Perm.Add(XMLItem.Option("DirCreate", this._permission.HasFlag(Permission.DirCreate) ? true : false));
            Perm.Add(XMLItem.Option("DirDelete", this._permission.HasFlag(Permission.DirDelete) ? true : false));
            Perm.Add(XMLItem.Option("DirList", this._permission.HasFlag(Permission.DirList) ? true : false));
            Perm.Add(XMLItem.Option("DirSubdirs", this._permission.HasFlag(Permission.DirSubdirs) ? true : false));

            Perm.Add(XMLItem.Option("IsHome", this._isHome));
            Perm.Add(XMLItem.Option("AutoCreate", this._autocreate));



            return Perm;
        }

    }
}
